local biz_name = "_adv.gif"

ConfigRedisConnectors[biz_name]={
    host = "127.0.0.1",
    port = 6379,
    unixpath = "/etc/redis/unixpath/redis.sock",
    db = NIL,
    user = NIL,
    passwd = NIL,
    queue = "queue_1"
}

local function _adv_gif()
  return coroutine.create(function ()
    -- constants
    local REDIS_SEPERATOR = config.SEPARATE_SYN    
    local lpush_cmd_format = "lpush "..ConfigRedisConnectors[biz_name].queue.." %s "
    local http_cookie_domain = config.http.setcookie.domain
    

    -- headers
    local local_config = config
    local COOKIE_HEADER = local_config.COOKIE_HEADER
    local USER_AGENT_HEADER = local_config.USER_AGENT_HEADER
    local SET_COOKIE_HEADER = local_config.SET_COOKIE_HEADER 
    local REFERER_HEADER = local_config.REFERER_HEADER 
    local SERVER_HEADER = local_config.SERVER_HEADER 
    local PRAGMA_HEADER = local_config.PRAGMA_HEADER
    local CACHE_CONTROL_HEADER = local_config.CACHE_CONTROL_HEADER
    local EXPIRES_HEADER = local_config.EXPIRES_HEADER
    local LAST_MODIFIED_HEADER = local_config.LAST_MODIFIED_HEADER
    local CONTENT_TYPE_HEADER = local_config.CONTENT_TYPE_HEADER
    --cookies
    local REGION_COOKIE = local_config.REGION_COOKIE
    local GUID_COOKIE = local_config.GUID_COOKIE
    local CREATE_COOKIE = local_config.CREATE_COOKIE 
    local EXPIRE_TIME = local_config.EXPIRE_TIME
    --_adv_gif--
    local ADM_DOMAIN_PARAM = "adm_domain"
    local ADM_ADITEM_PARAM = "adm_aditem"
    local ADM_ADITEM_STRING_DEFAULT = "12345678"
    local ADM_ZONEID_PARAM = "adm_zoneid"
    local ADM_ZONEID_STRING_DEFAULT = "-1"
    local ADM_CAMPAIGN_PARAM = "adm_campaign"
    local ADM_CAMPAIGN_STRING_DEFAULT = "-1"
    local ADM_CHANNELID_PARAM = "adm_channelid"
    local ADM_CHANNELID_STRING_DEFAULT = "-1"

     -- functions
    local local_reply = reply
    local local_request = request
    local local_luredis = luredis
    local os_time = os.time 
    local local_type = type
    local local_to_number = tonumber
    local local_util_redis_cmd = util.redis_command    
    
    while true do
	-- reply 
	local_reply.pushHeader(CACHE_CONTROL_HEADER, "private, no-cache, no-cache=Set-Cookie, proxy-revalidate")
        local_reply.pushHeader(PRAGMA_HEADER, "no-cache")
        local_reply.pushHeader(EXPIRES_HEADER, "Wed, 17 Sep 1975 21:32:10 GMT")
        local_reply.pushHeader(CONTENT_TYPE_HEADER, "image/gif")
        local_reply.pushHeader(LAST_MODIFIED_HEADER, "Wed, 19 Apr 2000 11:43:00 GMT")
        local_reply.pushHeader("P3P", "CP=\"IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA\"")
        local_reply.pushHeader("Content-Length", "0")
        local_reply.pushHeader("you", "yah")
        local_reply.setStatus(local_reply.status_type.ok)
	-- headers        
        local headerTable = local_request.getHeadersKV();
        local user_agent = headerTable[USER_AGENT_HEADER]
        local referer = headerTable[REFERER_HEADER];
	-- cookie 
        local cookieString = headerTable[COOKIE_HEADER];
        local cookieTable = parseCookies(cookieString);
        local create_cookie = cookieTable[CREATE_COOKIE]
        local guid = cookieTable[GUID_COOKIE];
	
        local newguid = 0;
        local create_time = os_time()
        local ip = local_request.getIP()

        if not guid then
	    guid = create_guid(create_time, ip);
	    create_cookie = create_time
	    expire_time = EXPIRE_TIME+create_time;	    
            newguid = 1;
	    pushCookieHeader(GUID_COOKIE, guid, expire_time, "/", http_cookie_domain, false)
	    pushCookieHeader(CREATE_COOKIE, create_time, expire_time, "/", http_cookie_domain, false)	   
        end        

        local paraTable = local_request.getParamsKV("&=");      
        local aditem = paraTable[ADM_ADITEM_PARAM];
        if not local_to_number(aditem) then
             aditem = ADM_ADITEM_STRING_DEFAULT
        end

        local adm_domain = fast_url_decode(paraTable[ADM_DOMAIN_PARAM])
        local zoneID = paraTable[ADM_ZONEID_PARAM];
        if not local_to_number(zoneID)  then
               zoneID = ADM_ZONEID_STRING_DEFAULT
        end

        local campaign = paraTable[ADM_CAMPAIGN_PARAM];
        if not local_to_number(campaign) then
               campaign = ADM_CAMPAIGN_STRING_DEFAULT
        end

        local chanelID = paraTable[ADM_CHANNELID_PARAM];
        if not local_to_number(chanelID) then
               chanelID = ADM_CHANNELID_STRING_DEFAULT
        end

        local_luredis.clear_buffer()
        local_luredis.concat_buffer(REDIS_SEPERATOR,
          create_time, ip, user_agent, create_cookie,
          aditem,  1,  guid, adm_domain,  0,  0,
          zoneID,  campaign,  chanelID,  newguid,  referer)
        local_util_redis_cmd(biz_name,false,local_luredis.send_buffer,lpush_cmd_format)
        coroutine.yield()
        end
      end)
    end

creators[biz_name] = function()
    f[biz_name] = _adv_gif()
end
